﻿using Glorysoft.Auto.Xfw;
using Glorysoft.EAP.Core;
using Glorysoft.EAP.EAPClient.Common;
using Glorysoft.EAP.EAPClient.Model;
using Serilog.Core;
using System;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Effects;

namespace Glorysoft.EAP.EAPClient
{
    /// <summary>
    /// 消息对话框按钮样式
    /// </summary>
    public enum ButtonStyles
    {
        NormalButtonStyle = 0,
        NotNormalButtonStyle = 1
    }
    /// <summary>
    /// MessageBoxWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AlarmMessageBoxWindow : Window
    {
        private readonly Logger ServiceLogger = XfwContext.EapLogger;
        public Model.RunService ClientInfo
        {
            get
            {
                return Model.RunService.Current;
            }
        }
        #region 成员
        private Style normalButtonStyle;
        private Style notNormalButtonStyle;
        #endregion
        #region 属性
        public string MessageBoxTitle
        {
            get;
            set;
        }
        public string MessageBoxText
        {
            get;
            set;
        }
        public string ImagePath
        {
            get;
            set;
        }
        public Visibility OKButtonVisibility
        {
            get;
            set;
        }
        public Visibility CancelButtonVisibility
        {
            get;
            set;
        }
        public Visibility YesButtonVisibility
        {
            get;
            set;
        }
        public Visibility NoButtonVisibility
        {
            get;
            set;
        }
        public ButtonStyles OKButtonStyle
        {
            set
            {
                if (value == ButtonStyles.NormalButtonStyle)
                {
                    OKButton.Style = normalButtonStyle;
                }
                else if (value == ButtonStyles.NotNormalButtonStyle)
                {
                    OKButton.Style = notNormalButtonStyle;
                }
            }
        }
        public ButtonStyles CancelButtonStyle
        {
            set
            {
                if (value == ButtonStyles.NormalButtonStyle)
                {
                    CancelButton.Style = normalButtonStyle;
                }
                else if (value == ButtonStyles.NotNormalButtonStyle)
                {
                    CancelButton.Style = notNormalButtonStyle;
                }
            }
        }
        public ButtonStyles YesButtonStyle
        {
            set
            {
                if (value == ButtonStyles.NormalButtonStyle)
                {
                    YesButton.Style = normalButtonStyle;
                }
                else if (value == ButtonStyles.NotNormalButtonStyle)
                {
                    YesButton.Style = notNormalButtonStyle;
                }
            }
        }
        public ButtonStyles NoButtonStyle
        {
            set
            {
                if (value == ButtonStyles.NormalButtonStyle)
                {
                    NoButton.Style = normalButtonStyle;
                }
                else if (value == ButtonStyles.NotNormalButtonStyle)
                {
                    NoButton.Style = notNormalButtonStyle;
                }
            }
        }
        public AlarmMessageBoxResult Result;
        #endregion
        #region 构造函数
        public AlarmMessageBoxWindow()
        {
            try
            {
                InitializeComponent();
                this.DataContext = this;
                WindowStartupLocation = WindowStartupLocation.CenterOwner;             
                this.Topmost = true;
                Rect rc = SystemParameters.WorkArea;//获取工作区大小 
                this.Left = (rc.Width) * 4 / 6;//设置位置 
                this.Top = (rc.Height) / 3;
                MessageBoxTitle = "报警消息提示";
                OKButtonVisibility = System.Windows.Visibility.Collapsed;
                CancelButtonVisibility = System.Windows.Visibility.Hidden;
                YesButtonVisibility = System.Windows.Visibility.Collapsed;
                NoButtonVisibility = System.Windows.Visibility.Collapsed;
                normalButtonStyle = this.FindResource("NormalButtonStyle") as Style;
                notNormalButtonStyle = this.FindResource("NotNormalButtonStyle") as Style;
                Result = AlarmMessageBoxResult.None;
            }
            catch (Exception ex)
            {
                ServiceLogger.Error(string.Format("{0}: {1}", GetType().Name, ex));
            }
        }
        #endregion
        #region 事件
        private void OnMouseLeftButtonDownAtTitlee(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            Result = AlarmMessageBoxResult.OK;
            //人为点击OK按钮后，上报状态给server
            EAPClient eapclient = new EAPClient(); 
            this.Close();
            eapclient.ClearAlarm("0");
            Dict.Current.GPIBAlarmCount = 0;
            ServiceLogger.Debug("send clear alarm box ");
        }
        private void YesButton_Click(object sender, RoutedEventArgs e)
        {
            Result = AlarmMessageBoxResult.Yes;
            this.Close();
        }
        private void NoButton_Click(object sender, RoutedEventArgs e)
        {
            Result = AlarmMessageBoxResult.No;
            this.Close();
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Result = AlarmMessageBoxResult.Cancel;
            this.Close();
        }
        private void CloseWindowButton_Click(object sender, RoutedEventArgs e)
        {
            Result = AlarmMessageBoxResult.None;
            this.Close();
        }
        private void Win_Loaded(object sender, RoutedEventArgs e)
        {
            BlurEffect effect = new BlurEffect();
            effect.Radius = 0.2;
            effect.KernelType = KernelType.Gaussian;
            Application.Current.Windows[0].Effect = effect;
           
        }
        private void Win_Closed(object sender, EventArgs e)
        {
            Application.Current.Windows[0].Effect = null;
        }
        #endregion
    }
}
